﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using ZCrawler;

namespace ZCrawlerManager
{
    public partial class FrmCrawlProcess : DevExpress.XtraEditors.XtraForm
    {
        public ZCrawlDataProcessor processor { get; set; }
        public DialogResult result { get; set; }
        private string baseUrl;
        public FrmCrawlProcess()
        {
            InitializeComponent();
        }
        public FrmCrawlProcess(ZCrawlDataProcessor p,string b_url)
        {
            InitializeComponent();
            processor = p;
            baseUrl = b_url;
            result = System.Windows.Forms.DialogResult.Cancel;
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            try
            {
                string[] param = txtRule.Text.Split('&');
                ZCrawlDataProcessorType processorType = (ZCrawlDataProcessorType)Enum.Parse(typeof(ZCrawlDataProcessorType), cmbProcessor.SelectedItem.ToString());
                switch (processorType)
                {
                    case ZCrawlDataProcessorType.Trim:
                        TrimMethod method = TrimMethod.Both;
                        method = (TrimMethod)int.Parse(param[0].Split('=')[1].ToString());
                        string str = param[1].Split('=')[1].ToString();
                        ((ZCrawlDataTrimmingProcessor)processor).method = method;
                        ((ZCrawlDataTrimmingProcessor)processor).StringToTrim = str;
                        break;
                    case ZCrawlDataProcessorType.StringSpliterFormatter:
                        string spliter = param[0].Split('=')[1].ToString();
                        string formatter = param[1].Split('=')[1].ToString();
                        ((ZCrawlDataSpliterFormatterProcessor)processor).ori_spliter = spliter;
                        ((ZCrawlDataSpliterFormatterProcessor)processor).formatter = formatter;
                        break;
                    case ZCrawlDataProcessorType.Replace:
                        string ori_str = param[0].Split('=')[1].ToString();
                        string to_str = param[1].Split('=')[1].ToString();
                        ((ZCrawlDataReplaceProcessor)processor).ori_str = ori_str;
                        ((ZCrawlDataReplaceProcessor)processor).to_str = to_str;
                        break;
                }
                result = System.Windows.Forms.DialogResult.OK;
                this.Close();
            }
            catch(Exception ex)
            {
                XtraMessageBox.Show(ex.Message);
                return;
            }
        }

        private void FrmCrawlProcess_Load(object sender, EventArgs e)
        {
            cmbProcessor.Properties.Items.AddRange(Enum.GetNames(typeof(ZCrawlDataProcessorType)));
        }

        private void cmbProcessor_SelectedValueChanged(object sender, EventArgs e)
        {
            ZCrawlDataProcessorType processorType = (ZCrawlDataProcessorType)Enum.Parse(typeof(ZCrawlDataProcessorType), cmbProcessor.SelectedItem.ToString());
            switch(processorType)
            {
                case ZCrawlDataProcessorType.Trim:
                    processor = new ZCrawlDataTrimmingProcessor();
                    txtRule.Text = "fromStart=&trimmingString=";
                    break;;
                case ZCrawlDataProcessorType.AbsoluteUrl:
                    processor = new ZCrawlDataAbsoluteUrlProcessor()
                    {
                        originalUrl = baseUrl
                    };
                    break;
                case ZCrawlDataProcessorType.HtmlDecode:
                    processor = new ZCrawlDataHtmlDecodeProcessor();
                    break;
                case ZCrawlDataProcessorType.HtmlTagRemoval:
                    processor = new ZCrawlDataCleanHtmlTagProcessor();
                    break;
                case ZCrawlDataProcessorType.ImgUrlParser:
                    processor = new ZCrawlDataGetImgUrlProcessor();
                    break;
                case ZCrawlDataProcessorType.StringSpliterFormatter:
                    processor = new ZCrawlDataSpliterFormatterProcessor();
                    txtRule.Text = "spliter=&formatter=";
                    break;
                case ZCrawlDataProcessorType.HrefParser:
                    processor = new ZCrawlDataHrefParser();
                    break;
                case ZCrawlDataProcessorType.Replace:
                    processor = new ZCrawlDataReplaceProcessor();
                    txtRule.Text = "ori=&to=";
                    break;
            }
        }
    }
}